//
//  MNotificationCell.m
//  Mask
//
//  Created by 郭子一 on 2020/10/4.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MNotificationCell.h"

@implementation MNotificationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = UIColor.clearColor;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    UIView *view = [[UIView alloc] init];
    view.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    view.layer.cornerRadius = 10;
    view.layer.shadowColor = [UIColor colorWithRed:93/255.0 green:102/255.0 blue:147/255.0 alpha:0.15].CGColor;
    view.layer.shadowOffset = CGSizeMake(0,2);
    view.layer.shadowOpacity = 1;
    view.layer.shadowRadius = 8;
    [self.contentView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(8);
        make.bottom.offset(-7);
        make.left.offset(15);
        make.right.offset(-15);
    }];
    
    UIImageView *notiImg = [[UIImageView alloc] init];
    notiImg.image = [UIImage imageNamed:@"Notification"];
    [view addSubview:notiImg];
    [notiImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(40);
    }];
    
    self.titleLab = [UILabel new];
    self.titleLab.font = JSS_FONT_4;
    self.titleLab.textColor = RGB_VALUE(0x2E2F33);
    [view addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(notiImg.mas_right).offset(10);
        make.right.offset(-20);
        make.height.mas_equalTo(14);
        make.top.equalTo(notiImg.mas_top);
    }];
    
    self.contentLab = [UILabel new];
    self.contentLab.numberOfLines = 0;
    self.contentLab.font = JSS_FONT_2;
    self.contentLab.textColor = RGB_VALUE(0x5C5D66);
    [view addSubview:self.contentLab];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.left.equalTo(notiImg.mas_right).offset(10);
        make.top.equalTo(self.titleLab.mas_bottom).offset(11);
        make.height.mas_equalTo(24);
    }];
}

+ (CGFloat)getHeightWithStr:(NSString *)text
{
    CGFloat height = [text boundingRectWithSize:CGSizeMake(kGScreenWidth - 120, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: JSS_FONT_2} context:nil].size.height;
    return height;
}

- (void)setContentString:(NSString *)contentString
{
    _contentString = contentString;
    CGFloat heights = [MNotificationCell getHeightWithStr:contentString] + 6;
    [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(heights);
    }];
}
@end
